//
//  CharOrderViewController.swift
//  funcode
//
//  Created by Wan Jiading on 2016/11/24.
//  Copyright © 2016年 WanJiading. All rights reserved.
//

import UIKit

class CharOrderViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

	var selectedItem: ((CountryArea) -> ())?
	
	let cellID = "CellID"
	var dataList: [CountryAreaList]!
	
	@IBOutlet weak var searchBar: UISearchBar!
	@IBOutlet weak var tableView: UITableView!
	
	@IBOutlet weak var searchBarTop: NSLayoutConstraint!
	
    override func viewDidLoad() {
        super.viewDidLoad()
		
		initData()
		setupSubViews()
    }
	
	func setupSubViews() {
		self.navigationController?.title = "字母排序"
		self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .plain, target: self, action: #selector(dismissSelf))
		
//		self.view.backgroundColor = UIColor.rgb("C3C3C3")
//		searchBar.tintColor = UIColor.rgb("84a740")
		
		
		
		searchBar.placeholder = "搜索国家和地区"
		////		searchBar.showsBookmarkButton = true // 书签。  不能和 ResultButton 同时存储在
		searchBar.showsCancelButton = false
////		searchBar.showsSearchResultsButton = true  // 结果列表。 不能和 BookmarkButton 同时存储在

		// 索引
		tableView.sectionIndexColor = UIColor.rgb("84a740")
//		tableView.sectionIndexMinimumDisplayRowCount = 20
		tableView.sectionIndexBackgroundColor = UIColor.clear
//		tableView.sectionIndexTrackingBackgroundColor = UIColor.red	// 选中时的背景色
	}
	
	func dismissSelf() {
		self.dismiss(animated: true, completion: {
		
		})
	}
	
	// MARK: - UITableViewDataSource
	
	func numberOfSections(in tableView: UITableView) -> Int {
		return dataList.count
	}
	
	func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return dataList[section].countryAreaList!.count
	}
	
	func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
		
		var cell = tableView.dequeueReusableCell(withIdentifier: cellID)
		if cell == nil {
			cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellID)
		}
		
		let country = dataList[indexPath.section].countryAreaList![indexPath.row]
		
		cell!.textLabel?.text = country.area_name
		cell!.detailTextLabel?.text = country.area_code
		
		return cell!
	}
	
	func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
		return 60
	}
	
	func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
		let country = dataList[indexPath.section].countryAreaList![indexPath.row]
		selectedItem?(country)
		dismissSelf()
	}
	
	// MARK: SectionHeaderTitle
	func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
		return dataList[section].letter
	}
	
	
	
	// MARK: - 右边的索引
	
	/// 索引显示
	func sectionIndexTitles(for tableView: UITableView) -> [String]? {
		
		var firstCharList = [String]()
		for c in dataList {
			firstCharList.append(c.letter!)
		}
		
		return firstCharList
	}
	
	/// 索引被点击
	func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
		DebugLog("\(index)    \(title)")
		
//		let selectIndexPath = IndexPath(row: 0, section: index)
//		tableView.scrollToRow(at: selectIndexPath, at: .bottom, animated: true)
		
		return index
	}
	
	// MARK: - SearchBarDelegate
	
	/// 能否获取焦点
	func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
		DebugLogFunc()

		searchBar.setShowsCancelButton(true, animated: true)
//		SharedApplication.setStatusBarHidden(true, with: UIStatusBarAnimation.slide)
		UIView.animate(withDuration: 0.25, animations: { [unowned self] in
			self.navigationController?.navigationBar.frame.origin.y = -64
			self.searchBarTop.constant = 20
			self.view.layoutIfNeeded()
		})
		
		return true
	}
	
	
	/// 获得焦点
	func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
		DebugLogFunc()
	}
	
	/// 能否修改
	func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
		DebugLogFunc()
		return true
	}
	
	/// 内容发生改变
	func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
		DebugLogFunc()
	}
	
	
	func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
		DebugLogFunc()
	}
	
	func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
		DebugLogFunc()
		
		searchBar.setShowsCancelButton(false, animated: true)
		UIView.animate(withDuration: 0.25, animations: { [unowned self] in
			self.navigationController?.navigationBar.frame.origin.y = 20
			self.searchBarTop.constant = 64
			self.view.layoutIfNeeded()
		})
		
		self.searchBar.resignFirstResponder()
	}
	
	/// 点击键盘上的 搜索按钮
	func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
		DebugLogFunc()
	}
	
	/// 书签按钮
	func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
		DebugLogFunc()
	}
	
	/// 结果列表
	func searchBarResultsListButtonClicked(_ searchBar: UISearchBar) {
		DebugLogFunc()
	}
	
	func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
		DebugLogFunc()
		return true
	}
	
	
	
	func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
		DebugLogFunc()
	}
	
	
	
	
	
	
	
	// MARK: - 初始化数据
	
	func initData() {
		
		dataList = [
			
		]
	}
	
}
